package btb.net;

import java.util.ArrayList;

import btb.engine.CSession;
import btb.engine.CSession.NSessionProfile;
import btb.net.ANetDossier.NANetProfile;
import btb.net.CChannel.NChannelProfile;
import btb.util.AObjectList;
import btb.util.AbstractObjectList;
import btb.util.CLogger;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.EndPoint;

public class CNetwork {
	public static int NULL				= -1;
	
	public static int SERVER			= 10;
	public static int GAME_SERVER 		= 11;
	public static int CHAT_SERVER 		= 12;
	public static int META_SERVER 		= 13;
	
	public static int CLIENT			= 20;
	public static int GAME_CLIENT 		= 21;
	public static int CHAT_CLIENT		= 22;
	public static int META_CLIENT		= 23;
	
	public static String META_SERVER_HOST = "localhost";
	public static String CHAT_SERVER_HOST = "localhost";
	
	public static int GAME_SERVER_PORT = 6668;
	public static int CHAT_SERVER_PORT = 6667;
	public static int META_SERVER_PORT = 6666;
	
	/** BEGIN SERVER MESSAGE CLASSES **/
	
	public static class NRegister {
		
	}
	
	public static class NUpdate {
		
	}
	
	public static class NValidate {
		public String validationKey;
	}
	
	/** END SERVER MESSAGE CLASSES **/
	
	/** BEGIN META_SERVER MESSAGE CLASSES **/
	
	public static class NAuthenticate {
		public String user;
		public String pass;
	}
	
	public static class NRegisterMeta extends NRegister {
		public int networkType;
		public String name;
	}
	
	public static class NRegisterServer extends NRegisterMeta {
		public String IP;
	}
	
	public static class NRegisterClient extends NRegisterMeta {
		
	}
	
	public static class NUpdateServerList extends NUpdate
	{
		public String[] names;
	}
	
	public static class NUpdateUserList extends NUpdate
	{
		public String[] names;
	}
	
	public static class NUpdateChannelList extends NUpdate
	{
		public ArrayList channels;
	}
	
	/** END META_SERVER MESSAGE CLASSES **/
	
	/** BEGIN CHAT_SERVER MESSAGE CLASSES **/
	
	public static class NChatMessage
	{
		public String channelName;
		public String senderName;
		public String text;
	}
	
	public static class NSubscribe
	{
		public CUser user;
		public String channelName;
	}
	
	public static class NUnsubscribe
	{
		public String userName;
		public String channelName;
	}

	public static class NChatOpResults
	{
		public static final int SEND_MSG_ATTEMPT	= 0;
		public static final int SUBSCRIBE_ATTEMPT 	= 1;
		public static final int UNSUBSCRIBE_ATTEMPT = 2;
	
		public String channelName;
		public int operation;
		public boolean success;
	}
	
	/** END CHAT_SERVER MESSAGE CLASSES **/
	
	protected static class NServerList extends AbstractObjectList
	{
		
	};
	
	public static class NClientList extends AbstractObjectList
	{
		public int findClientByName(String _username)
		{
			int pos = -1;
			for(int i = 0; i < this.list.length; i++)
			{
				if(((CUser) this.list[i]).getProfile().toString().equals(_username))
				{
					pos = i;
				}
			}
			
			if(pos < 0)
				CLogger.cond_trace("metaserver", "findClientByName: '" + _username + "' : " + pos);
				
			return pos;
		}
	};
	
	protected static class NChannelMap extends AObjectList
	{
		public int findChannelByName(String _channelName)
		{
			int pos = -1;
			for(int i = 0; i < this.list.length; i++)
			{
				if(((CChannel) this.list[i]).getProfile().toString().equals(_channelName))
				{
					pos = i;
				}
			}
			
			if(pos < 0)
				CLogger.cond_trace("chat server", "findChannelByName: '" + _channelName + "' : " + pos);
				
			return pos;
		}
	}
	
	public static void register(EndPoint _endPoint)
	{
		Kryo kryo = _endPoint.getKryo();
		kryo.register(ArrayList.class);
		kryo.register(NRegister.class);
		kryo.register(NUpdate.class);
		kryo.register(NValidate.class);
		kryo.register(NAuthenticate.class);
		kryo.register(NRegisterMeta.class);
		kryo.register(NRegisterServer.class);
		kryo.register(NRegisterClient.class);
		kryo.register(NUpdateServerList.class);
		kryo.register(NUpdateUserList.class);
		kryo.register(CSession.class);
		kryo.register(NSessionProfile.class);
		kryo.register(CChannel.class);
		kryo.register(NANetProfile.class);
		kryo.register(NChannelProfile.class);
//		kryo.register(btb.net.CChannel.NChannelProfile[].class);
		kryo.register(NUpdateChannelList.class);
		kryo.register(NChatMessage.class);
		kryo.register(NChatOpResults.class);
	}
}
